package com.cxhc.app.api;


import com.cxhc.app.api.vo.PostExaminationVo;
import com.cxhc.common.annotation.Anonymous;
import com.cxhc.common.core.domain.R;
import com.cxhc.common.utils.SecurityUtils;
import com.cxhc.system.domain.app.*;
import com.cxhc.system.service.app.IExaminationService;
import com.cxhc.system.service.app.IExaminationUserService;
import com.cxhc.system.service.app.IQuestionBankService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 首页模块接口
 */
@RestController()
@RequestMapping("/api/examination")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ApiExamination {

    @Resource
    private IExaminationService examinationService;

    @Resource
    private IQuestionBankService questionBankService;

    @Resource
    private IExaminationUserService examinationUserService;

    /**
     * 考试场次列表
     *
     * @return
     */
    @GetMapping("/list")
    private R<List<Examination>> list(Integer status) {
        Long userId = SecurityUtils.getUserId();
        List<Examination> examinationList = examinationService.getAllExamination(new Examination().setStatus(status));
        // 获取我考试的场次
        examinationList.forEach(examination -> {
            examination.setExaminationUser(examinationUserService.selectExaminationUserByUserIdAndExaminationId(userId, examination.getId()));
        });
        return R.ok(examinationList);
    }

    /**
     * 考试考题
     *
     * @return
     */
    @GetMapping("/questionList/{examinationId}")
    private R<List<QuestionBank>> questionList(@PathVariable Long examinationId) {
        return R.ok(questionBankService.selectQuestionBankListByExamId(examinationId));
    }

    /**
     * 用户试卷信息
     *
     * @return
     */
    @GetMapping("/userExamination/detaile")
    private R<List<ExaminationUserQuestion>> questionList(String userId, String examinationId) {

        return R.ok(examinationUserService.selectDetailesByUserIdAndExaminationId(userId,examinationId));
    }

    /**
     * 考试提交答案
     *
     * @return
     */
    @PostMapping("/postExam")
    private R<String> postExam(@RequestBody @Valid PostExaminationVo vo) {
        Long userId = SecurityUtils.getUserId();
        int row = examinationUserService.insertExaminationUser(new ExaminationUser()
                .setExaminationId(vo.getExaminationId())
                .setUserId(userId)
                .setTime(vo.getTime())
                .setScore(vo.getScore())
                .setTotalScore(vo.getTotalScore())
                .setQuantity(vo.getQuestionList().size()));
        if (row == 1) {
            examinationService.submit(vo.getQuestionList());
            return R.ok();
        }
        return R.fail("你已考过本次考试");
    }

    /**
     * 获取我的考试场次
     *
     * @param examinationId
     * @return
     */
    @GetMapping("/mineExamination/{examinationId}")
    private R<ExaminationUser> getMineExamination(@PathVariable Long examinationId) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(examinationUserService.selectExaminationUserByUserIdAndExaminationId(userId, examinationId));
    }


    /**
     * 计算用户考试分数
     *
     * @return
     */
    @Anonymous
    @RequestMapping("/calculateScore")
    private R<Float> calculateScore(String userId, String examinationId) {
        return R.ok(examinationService.calculateScore(userId, examinationId));
    }

    /**
     * 根据用户查询考试
     *
     * @return
     */
    @Anonymous
    @RequestMapping("/queryExaminationByUserId")
    @ApiOperation("/queryExaminationByUserId")
    private R<List<Examination>> queryExaminationByUserId(String userId) {
        return R.ok(examinationService.queryExaminationByUserId(userId));
    }

}
